import 'package:app/common_widget/colors.dart';
import 'package:app/net/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../common_widget/countdown_button_page.dart';
import '../common_widget/hexcolor.dart';
import '../common_widget/phone_inputformatter.dart';
import 'package:get/get.dart';

import '../net/address.dart';
class ForgetPassword extends StatefulWidget {
  const ForgetPassword({Key? key}) : super(key: key);

  @override
  State<ForgetPassword> createState() => _ForgetPasswordState();
}

class _ForgetPasswordState extends State<ForgetPassword> {


  final TextEditingController _mobileTextController = TextEditingController();

  final TextEditingController _codeTextController = TextEditingController();
  final TextEditingController _passWordController = TextEditingController();
  final TextEditingController _passWordControlle1 = TextEditingController();

  var options = '+86';



  requestDataWithChangePassword(var mobile,var password,var code)async{

    var params = {
      'mobile':mobile.replaceAll(new RegExp(r"\s+\b|\b\s"), ""),
      'password':password,
      'code':code,
    };

    var json = await DioManager().kkRequest(Address.forgetPassword,bodyParams: params);
    if(json['errmsg']=='ok'){
      BotToast.showText(text: '${json['message']}');
      Get.back();
    }
    BotToast.showText(text: '${json['message']}');

  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('忘记密码',style: TextStyle(color: Colors.black),),
        leading: IconButton(onPressed: () {
          Get.back();
        },icon: Image.asset('images/leftarr.png',width:15,height: 15,),),
      ),
      body: Container(
        child: ListView(
          children: [
            Center(
              child: Container(

                  margin: EdgeInsets.only(top: 20, left: 15, right: 10),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(

                          // margin: EdgeInsets.only(left: 15),
                          width: 200,
                          child: Row(
                            children: [
                              GestureDetector(
                                onTap: () async {
                                  // var data = await  Get.to(SelectOptionsPage());
                                  // if(data!=null){
                                  //   options = data;
                                  //   setState(() {
                                  //
                                  //   });
                                  // }

                                },
                                child: Container(
                                  margin: EdgeInsets.only(top: 2),
                                  // color: Colors.red,
                                  child: Text(
                                    '${options}', style: TextStyle(color: Theme
                                      .of(context)
                                      .primaryColor),),
                                ),
                              ),
                              // TextButton(onPressed: (){
                              //   Get.to(SelectOptionsPage());
                              //
                              // }, child: Text('+86')),

                              Container(
                                padding: const EdgeInsets.only(left: 5),
                                width: 160,
                                child: TextField(
                                  controller: _mobileTextController,
                                  keyboardType: TextInputType.phone,
                                  style: TextStyle(
                                      fontSize: 16, color: HexColor('#333333')),
                                  inputFormatters: <TextInputFormatter>[
                                    LengthLimitingTextInputFormatter(13), //限制长度
                                    phoneInputFormatter(),
                                  ],
                                  decoration: InputDecoration(
                                    hintText: '请输入手机号码'.tr,
                                    hintStyle: TextStyle(fontSize: 16,
                                        color: HexColor('#BBBBBB')),
                                    border: InputBorder.none,
                                    // prefixIcon: Icon(Icons.phone_android,size: 15,),
                                  ),
                                ),
                              )
                            ],
                          )
                      ),
                      CountdownButton(_mobileTextController,type: 'pass',),
                    ],
                  )
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 15,right: 15),
              color: AppColor.lineColor,
              width: Get.width,
              height: 1,
            ),
            Center(
              child: Container(
                alignment: Alignment.center,
                // color: Colors.red,
                // margin: EdgeInsets.only(left: 25),
                margin: EdgeInsets.only(left: 15,right: 15,top: 15),
                child:Row(
                  children: [
                    Image.asset('images/yzm_icon.png',width: 16,height: 16,),
                    Container(
                     // color: Colors.yellow,
                     width: Get.width-60,
                     height: 40,
                     margin: EdgeInsets.only(left: 10),
                     child:  TextField(
                       inputFormatters: <TextInputFormatter>[
                         LengthLimitingTextInputFormatter(6)//限制长度
                       ],
                       controller: _codeTextController,
                       style: TextStyle(fontSize: 16, color: HexColor('#333333')),
                       decoration: InputDecoration(
                         hintText: '请输入验证码'.tr,
                         hintStyle: TextStyle(fontSize: 16, color: HexColor(
                             '#BBBBBB')),
                         border: InputBorder.none,
                         // prefixIcon: Image.asset(A.assets_pwd),
                       ),
                     ),
                   )
                  ],
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 15,right: 15),
              color: AppColor.lineColor,
              width: Get.width,
              height: 1,
            ),
            Center(
              child: Container(
                // color: Colors.red,
                margin: EdgeInsets.only(top: 15, right: 15, left: 15),
                child: Container(
                  // margin: EdgeInsets.only(left: 25),
                  child: Row(
                    children: [
                      Image.asset('images/pass_icon.png',width: 16,height: 16,),
                      Container(

                        margin: EdgeInsets.only(left: 5),
                        height: 40,
                        width: Get.width-60,
                        child: TextField(
                          obscureText: true,
                          inputFormatters: <TextInputFormatter>[
                            LengthLimitingTextInputFormatter(16)//限制长度
                          ],
                          controller: _passWordController,
                          style: TextStyle(fontSize: 16, color: HexColor('#333333')),
                          decoration: InputDecoration(
                            hintText: '请输入新密码'.tr,
                            hintStyle: TextStyle(fontSize: 16, color: HexColor(
                                '#BBBBBB')),
                            border: InputBorder.none,
                            // prefixIcon: Image.asset(A.assets_pwd),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 15,right: 15),
              height: 1,
              color: AppColor.lineColor,
            ),

            Center(
              child: Container(
                // color: Colors.red,
                margin: EdgeInsets.only(top: 15, right: 15, left: 15),
                child: Container(
                  // margin: EdgeInsets.only(left: 25),
                  child: Row(
                    children: [
                      Image.asset('images/pass_icon.png',width: 16,height: 16,),
                      Container(
                        height: 40,
                        width: Get.width-60,

                        margin: EdgeInsets.only(left: 5),
                        child: TextField(
                          obscureText: true,
                          inputFormatters: <TextInputFormatter>[
                            LengthLimitingTextInputFormatter(16)//限制长度
                          ],
                          controller: _passWordControlle1,
                          style: TextStyle(fontSize: 16, color: HexColor('#333333')),
                          decoration: InputDecoration(
                            hintText: '确认密码'.tr,
                            hintStyle: TextStyle(fontSize: 16, color: HexColor(
                                '#BBBBBB')),
                            border: InputBorder.none,
                            // prefixIcon: Image.asset(A.assets_pwd),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 15,right: 15),
              height: 1,
              color: AppColor.lineColor,
            ),
            SizedBox(height: 15,),
            Center(
              child: Text('8-16位密码，须包含数字、字母、符号中至少2位元素',style: TextStyle(fontSize: 12,
              color: AppColor.smallTextColor
              ),),
            ),

            // Container(
            //   padding: EdgeInsets.only(left: 25, top: 15, right: 25),
            //   child: Row(
            //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
            //     children: [
            //       GestureDetector(
            //
            //         child: Text(
            //           '手势登录'.tr, style: TextStyle(fontSize: 14, color: HexColor(
            //             '#2A39E1')),),
            //         onTap: () {
            //           // Get.to(GesturePasswordWidgetDemo());
            //         },
            //       ),
            //       GestureDetector(
            //
            //         child: Text(
            //           '人脸登录'.tr, style: TextStyle(fontSize: 14, color: HexColor(
            //             '#2A39E1')),),
            //         onTap: () {
            //           // Get.to(FaceLoginPage());
            //         },
            //       ),
            //     ],
            //   ),
            // ),

            Container(
              margin: EdgeInsets.only(top: 72, right: 15, left: 15),
              width: Get.width,
              height: 45,
              child: MaterialButton(
                elevation: 0,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(22),
                ),
                color: Theme
                    .of(context)
                    .primaryColor,
                onPressed: () async {

                  if (_mobileTextController.text.isEmpty) {
                    // Toast.toast(context,msg: '请输入手机号码');
                    BotToast.showText(text: '请输入手机号码');
                    return;
                  }
                  if(!GetUtils.isPhoneNumber(_mobileTextController.text)){
                    BotToast.showText(text: '请输入正确的手机号码');
                    return;
                  }

                  if(_passWordController.text!=_passWordControlle1.text){
                    BotToast.showText(text: '两次密码不一样');
                    return;
                  }

                  requestDataWithChangePassword(_mobileTextController.text,
                      _passWordController.text, _codeTextController.text);
                },
                child: Text('修改密码'.tr, style: TextStyle(color: Colors.white),),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
